package com.ehome.user.Adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ehome.user.Interface.OnItemClickListener;
import com.ehome.user.R;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<String> mList;
    private OnItemClickListener onItemClickListener;
    private Integer[] icons = {R.mipmap.icon_3, R.mipmap.icon_5, R.mipmap.icon_6,R.mipmap.icon_7, R.mipmap.icon_8, R.mipmap.icon_9};

    public MyAdapter(Context context, List<String> list) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mList = list;
    }

    public void setOnClickListener(OnItemClickListener mOnClickListener) {
        onItemClickListener = mOnClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final MyViewHolder viewHolder = new MyViewHolder(mInflater.inflate(R.layout.recyclerview_item_layout, parent, false));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v);
            }
        });
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemClickListener.onItemLongClick(v);
                return true;
            }
        });

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder mViewHolder = (MyViewHolder) holder;
        mViewHolder.im_icon.setImageResource(icons[position]);
        mViewHolder.tv_title.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }

    public void removeItem(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }
}
